/*
 * DateChooser.java
 *
 * Created on January 20, 2005, 8:36 PM
 */

package com.regal.canlendar;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>A simple and easy to use Date Chooser, based on the MonthlyCalendar component.</p>
 *
 * <p>Usage:
 * <pre>
 *    DateChooser dc = new DateChooser(parentFrame, true/false);
 *    ...
 *    dc.setVisible(true); //this will initialise the calendar to current date
 *    Calendar cal = dc.getSelectedDate();  //get selected date
 *    if (cal == null) {
 *        //means no date selected (i.e. dialog closed by Close button)
 *    } else {
 *        //do whatever u want with the selected date.
 *    }
 * </pre></p>
 *
 * @author  hysun
 */
public class DateChooser extends javax.swing.JDialog {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Calendar calendar;
    private Calendar retCal;
    
    public DateChooser() {
        super();
        init();
    }
    
    public DateChooser(Dialog owner) {
        super(owner);
        init();
    }
    
    public DateChooser(Dialog owner, boolean modal) {
        super(owner, modal);
        init();
    }
    
    public DateChooser(Dialog owner, String title) {
        super(owner, title);
        init();
    }
    
    public DateChooser(Dialog owner, String title, boolean modal) {
        super(owner, title, modal);
        init();
    }
    
    public DateChooser(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
        init();
    }
    
    public DateChooser(Frame owner) {
        super(owner);
        init();
    }
    
    public DateChooser(Frame owner, boolean modal) {
        super(owner, modal);
        init();
    }
    
    public DateChooser(Frame owner, String title) {
        super(owner, title);
        init();
    }
    
    public DateChooser(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        init();
    }
    
    public DateChooser(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
        super(owner, title, modal, gc);
        init();
    }
    
    private void init() {
        initComponents();
        //init date
        calendar = Calendar.getInstance();
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        jComboBox1.setSelectedIndex(m);
        jSpinner1.setValue(calendar.getTime());
        monthCalendar.setYearMonth(y, m+1);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        basePanel = new javax.swing.JPanel();
        containerPanel = new javax.swing.JPanel();
        controlPanel = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jSpinner1 = new javax.swing.JSpinner();
        viewPanel = new javax.swing.JPanel();
        monthCalendar = new MonthlyCalendar() {
            /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			protected void dateSelected() {
                doSelection();
            }
        };

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Date Chooser");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        basePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 10));

        basePanel.setBackground(new java.awt.Color(252, 250, 252));
        containerPanel.setLayout(new java.awt.BorderLayout());

        containerPanel.setBorder(new javax.swing.border.TitledBorder("Date"));
        containerPanel.setOpaque(false);
        controlPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 12, 0));

        controlPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 0, 10, 0)));
        controlPanel.setOpaque(false);
        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 11));
        jComboBox1.setMaximumRowCount(7);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        controlPanel.add(jComboBox1);

        jSpinner1.setFont(new java.awt.Font("Tahoma", 0, 11));
        jSpinner1.setModel(new javax.swing.SpinnerDateModel());
        jSpinner1.setEditor(new javax.swing.JSpinner.DateEditor(jSpinner1, "yyyy"));
        jSpinner1.setPreferredSize(jComboBox1.getPreferredSize());
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        controlPanel.add(jSpinner1);

        containerPanel.add(controlPanel, java.awt.BorderLayout.NORTH);

        viewPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        viewPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(0, 0, 10, 0)));
        viewPanel.setOpaque(false);
        monthCalendar.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        viewPanel.add(monthCalendar);

        containerPanel.add(viewPanel, java.awt.BorderLayout.CENTER);

        basePanel.add(containerPanel);

        getContentPane().add(basePanel, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        // TODO add your handling code here:
        Date date = (Date) jSpinner1.getValue();
        calendar.setTime(date);
        monthCalendar.setYear(calendar.get(Calendar.YEAR));
        monthCalendar.repaint();
    }//GEN-LAST:event_jSpinner1StateChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == java.awt.event.ItemEvent.DESELECTED)
            return;
        monthCalendar.setMonth(jComboBox1.getSelectedIndex()+1);
        monthCalendar.repaint();
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        retCal = null;
    }//GEN-LAST:event_formWindowClosing
    
    /** Override parent setVisible method to include resetting of highlighted date */
    public void setVisible(boolean b) {
        monthCalendar.resetHighlight();
        super.setVisible(b);
    }
    
    /** Contains handling code for date selection event */
    private void doSelection() {
        retCal = monthCalendar.getSelectedDate();
        super.setVisible(false);
        dispose();
    }
    
    /**
     * User code can call this method to get the selected date as a Calendar object.
     * If the DateChooser dialog is closed by clicking the "Close" button, this
     * method returns null;
     * Else, the DateChooser dialog is closed by selecting a date, and the method 
     * will return a Calendar object representing that date.
     */
    public Calendar getSelectedDate() {
        return retCal;
    }
    
    /**
     * Set the calendar year and month to the specified value.
     * Should be called before set the date chooser visible.
     */
    public void setYearMonth(int y, int m) {
        if (y < 1970) {
            throw new IllegalArgumentException("Year must be later than 1970.");
        }
        if (m < 1 || m > 12) {
            throw new IllegalArgumentException("Invalid month.");
        }
        jComboBox1.setSelectedIndex(m-1);
        calendar.set(Calendar.YEAR, y);
        jSpinner1.setValue(calendar.getTime());
        monthCalendar.setYearMonth(y, m);
    }
    
    /**
     * Bean method to set start day of a week.
     * Will delegate to MonthlyCalendar.setWeekStartOnSunday(boolean) method.
     */
    public void setWeekStartOnSunday(boolean b) {
        monthCalendar.setWeekStartOnSunday(b);
    }
    
    /**
     * Bean method to get start day of a week.
     * Will delegate to MonthlyCalendar.getWeekStartOnSunday method.
     */
    public boolean getWeekStartOnSunday() {
        return monthCalendar.getWeekStartOnSunday();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel basePanel;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JSpinner jSpinner1;
    private MonthlyCalendar monthCalendar;
    private javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables
    
}
